﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Security.Cryptography;
using Microsoft.Windows.Controls;
using Incendios0_1.sms;

namespace Incendios0_1.user_managment
{
    /// <summary>
    /// Interaction logic for Adduser.xaml
    /// </summary>
    public partial class Adduser : Window
    {
        public Users User { get; set; }
        public Contacts Contact { get; set; }
        public Boolean AddContactFlag { get; set; }
        public String PasswordConfirmation { get; set; }
        private bool newContact;
        private bool newUser;

        public Adduser()
        {
            InitializeComponent();
            checkBox1.IsChecked = false;
            checkBox1.IsEnabled = true;
            User = new Users();
            Contact = new Contacts();
            passBox.Password = String.Empty;
            confirmPassBox.Password = String.Empty;
            main.DataContext = this;
            textBox3.IsEnabled = true;
            newContact = true;
            newUser = true;
        }

        public Adduser(Users user, Contacts contact)
        {
            InitializeComponent();
            this.User = user;
            passBox.Password = user.Password;
            confirmPassBox.Password = user.Password;
            newUser = false;
            if (contact != null)
            {
                newContact = false;
                this.Contact = contact;
                checkBox1.IsChecked = true;
                checkBox1.IsEnabled = false;
            }
            else
            {
                Contact = new Contacts();
                newContact = true;
                checkBox1.IsChecked = false;
                checkBox1.IsEnabled = true;
            }
            main.DataContext = this;
            textBox3.IsEnabled = false;
        }

        private void add_user_Click(object sender, RoutedEventArgs e)
        {
            User.Password = passBox.Password;
            PasswordConfirmation = confirmPassBox.Password;
            if (PasswordConfirmation.Equals(User.Password))
            {
                if (newUser)
                {
                    Contact.Name = User.Name;
                    UserControler.AddUser(User);
                    newUser = false;
                }
                if (!UserControler.SaveChanges())
                    Microsoft.Windows.Controls.MessageBox.Show("El usuario ya existe", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                else
                {
                    if (AddContactFlag && newContact)
                    {
                        Contact.UserId = Convert.ToInt32(User.UserId);
                        Contact.Name = User.Name;
                        ContactControler.AddContact(Contact);
                        if (!ContactControler.SaveChanges())
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("Imposible guardar contacto, el contacto ya existe, los datos del usuario han sido guardados", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        newContact = false;
                    }
                    Microsoft.Windows.Controls.MessageBox.Show("El usuario ha sido guardado", "Guardar", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("La contraseña no coincide", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void cancel_add_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
        }

        ~Adduser()
        {
            Cancel();
        }

        private void Cancel()
        {
            if (!newUser)
            {
                UserControler.Cancel(User);
            }
            if (!newContact)
            {
                ContactControler.Cancel(Contact);
            }
        }
    }
}
